package com.iot.ammeter.service.impl.web.equipment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iot.ammeter.constant.Constant;
import com.iot.ammeter.dto.web.equipment.EquipmentModelDTO;
import com.iot.ammeter.entity.web.equipment.EquipmentModel;
import com.iot.ammeter.exception.BusinessExceptions;
import com.iot.ammeter.mapper.web.equipment.EquipmentModelMapper;
import com.iot.ammeter.service.web.equipment.EquipmentModelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iot.ammeter.utils.SnowFlakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 设备型号 服务实现类
 * </p>
 *
 * @author basil
 * @since 2021-09-25
 */
@Service
public class EquipmentModelServiceImpl extends ServiceImpl<EquipmentModelMapper, EquipmentModel> implements EquipmentModelService {

    private final SnowFlakeIdWorker snowFlakeIdWorker;
    private final EquipmentModelMapper equipmentModelMapper;

    public EquipmentModelServiceImpl(SnowFlakeIdWorker snowFlakeIdWorker, EquipmentModelMapper equipmentModelMapper) {
        this.snowFlakeIdWorker = snowFlakeIdWorker;
        this.equipmentModelMapper = equipmentModelMapper;
    }

    /**
     * 添加设备型号
     *
     * @param equipmentModelDTO 设备型号信息
     * @return 添加成功或者添加失败提示
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String addEquipmentModel(EquipmentModelDTO equipmentModelDTO) {
        /**
         * 数据校验
         */
        String providerId = equipmentModelDTO.getProviderId();
        String equipmentModel = equipmentModelDTO.getEquipmentModel();
        Integer installedNumber = equipmentModelDTO.getInstalledNumber();
        Integer equipmentNumber = equipmentModelDTO.getEquipmentNumber();
        if (StringUtils.isEmpty(providerId)) {
            throw new BusinessExceptions("服务商id不能为空");
        }
        if (StringUtils.isEmpty(equipmentModel)) {
            throw new BusinessExceptions("设备型号不能为空");
        }
        if (installedNumber == null || installedNumber < 0) {
            throw new BusinessExceptions("已安装设备数量有误");
        }
        if (equipmentNumber == null || equipmentNumber < 0) {
            throw new BusinessExceptions("设备数量有误");
        }
        /**
         * 检查设备型号已经存在？
         */
        if (equipmentModelMapper.selectOne(new LambdaQueryWrapper<EquipmentModel>().eq(EquipmentModel::getEquipmentModel, equipmentModel)) != null) {
            throw new BusinessExceptions("设备型号已存在");
        }
        /**
         * 补全数据，保存数据库
         */
        EquipmentModel equipmentModelToDb = new EquipmentModel();
        equipmentModelToDb.setEquipmentModelId(String.valueOf(snowFlakeIdWorker.nextId()));
        equipmentModelToDb.setEquipmentModel(equipmentModel);
        equipmentModelToDb.setProviderId(providerId);
        equipmentModelToDb.setEquipmentNumber(equipmentNumber);
        equipmentModelToDb.setInstalledNumber(installedNumber);
        equipmentModelToDb.setCreateTime(LocalDateTime.now());
        equipmentModelToDb.setUpdateTime(LocalDateTime.now());
        equipmentModelToDb.setEquipmentStatus(Constant.EquipmentModel.NORMAL);
        int result = equipmentModelMapper.insert(equipmentModelToDb);
        return result > 0 ? "添加设备型号成功" : "添加设备型号失败";
    }

    /**
     * 获取设备型号列表
     *
     * @param page     请求页码
     * @param pageSize 每页数量
     * @return 返回设备型号列表
     */
    @Override
    public IPage<EquipmentModelDTO> listEquipmentModel(int page, int pageSize) {
        /**
         * 封装page对象
         */
        Page<EquipmentModelDTO> pages = new Page<>(page, pageSize);
        IPage<EquipmentModelDTO> iPage = equipmentModelMapper.listEquipmentModel(pages);
        return iPage;
    }
}
